'use strict';
const {parseTime, parseNum} = require("../../utils");
const {getDefaultModelConfig} = require("../../utils/common");
const {
    formatDate
} = require('../../utils/index.js');
module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL, BIGINT, TEXT } = app.Sequelize;
    const TravelOrder = app.model.define(
        'TravelOrder',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            travel_share_user_id: {type: INTEGER, allowNull: true, comment: '分享用户ID'},
            travel_share_order_id: {type: INTEGER, allowNull: true, comment: '代付订单ID'},
            travel_id: {type: INTEGER, allowNull: false, comment: '旅游产品ID'},
            travel_sku_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU ID'},
            travel_sku_stock_id: {type: INTEGER, allowNull: true, comment: '旅游产品SKU库存ID'},
            travel_name: {type: STRING(255), allowNull: false, comment: '旅游产品名称'},
            travel_cover: {type: STRING(500), allowNull: true, comment: '旅游产品封面'},
            sku_spec: {type: TEXT, allowNull: true, comment: 'SKU规格信息，JSON格式'},
            travel_date: {
                type: DATE,
                allowNull: false,
                comment: '旅游日期',
                get() {
                    return formatDate(this.getDataValue('travel_date'), 'YYYY-MM-DD');
                }
            },
            is_group: {
                type: TINYINT,
                defaultValue: 0,
                comment: '是否拼团：0-否，1-是'
            },
            order_no: STRING(255),
            user_id: BIGINT,
            // 旅游产品特有字段
            adult_count: {type: INTEGER, defaultValue: 0, comment: '成人数量'},
            child_count: {type: INTEGER, defaultValue: 0, comment: '儿童数量'},
            passenger_list: {type: TEXT, comment: '人员列表，JSON字符串格式'},
            contact_name: {type: STRING(50), comment: '联系人姓名'},
            contact_phone: {type: STRING(11), comment: '联系人电话'},
            remark: STRING(255),
            room_count: {type: INTEGER, defaultValue: 0, comment: '房间数量'},
            single_price_diff: {type: DECIMAL(10, 2), defaultValue: 0.00, comment: '单房差价格'},
            single_room_diff_amount: {type: DECIMAL(10, 2), defaultValue: 0.00, comment: '单房差总金额'},
            status: {
                type: STRING,
                defaultValue: 'pending'
            },
            // 旅游产品无需快递相关字段
            time_expire: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('time_expire'));
                }
            },
            count: {type: INTEGER, defaultValue: 1},
            amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('amount'));
                }
            },
            prepay_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '预付金额',
                get() {
                    return parseNum(this.getDataValue('prepay_amount'));
                }
            },
            payment_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('payment_time'));
                }
            },
            payment_status: {type: TINYINT, defaultValue: 0, comment: '支付状态：0-未支付，1-已支付'},
            payment_type: STRING(50),
            payment_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('payment_amount'));
                }
            },
            // MySQL新增字段脚本:
            // ALTER TABLE travel_orders ADD COLUMN hotel_level_name VARCHAR(255) COMMENT '酒店级别名称';
            // ALTER TABLE travel_orders ADD COLUMN hotel_level_price DECIMAL(10,2) DEFAULT 0.00 COMMENT '酒店级别价格';
            // ALTER TABLE travel_orders ADD COLUMN hotel_level_count INT DEFAULT 0 COMMENT '酒店级别数量';
            // ALTER TABLE travel_orders ADD COLUMN hotel_level_amount DECIMAL(10,2) DEFAULT 0.00 COMMENT '酒店级别总价';
            
            // 酒店级别名称
            hotel_level_name: STRING(255),
            // 酒店级别价格
            hotel_level_price: {type: DECIMAL(10, 2), defaultValue: 0.00},
            // 酒店级别数量
            hotel_level_count: {type: INTEGER, defaultValue: 0},
            // 酒店总价
            hotel_level_amount: {type: DECIMAL(10, 2), defaultValue: 0.00},
            // 佣金 成人
            travel_share_price: {type: DECIMAL(10, 2), defaultValue: 0.00},
            // 佣金 儿童
            travel_share_price2: {type: DECIMAL(10, 2), defaultValue: 0.00},
            transaction_id: STRING(255),
            prepay_id: STRING(255),
            refund_no: STRING(255),
            refund_transaction_id: STRING(255),
            refund_status: {type: TINYINT, defaultValue: -1, comment: '退款状态：-1-未退款，0-退款失败，1-待处理 2退款中 3 退款成功'},
            refund_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_time'));
                }
            },
            refund_success_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_success_time'));
                }
            },
            refund_reason: STRING(255),
            refund_remark: STRING(255),
            refund_result: STRING(255),
            refund_amount: {type: DECIMAL(10, 2), defaultValue: 0.00},
            cancel_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('cancel_time'));
                }
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'travel_order',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    TravelOrder.associate = function() {
        app.model.Business.TravelOrder.belongsTo(app.model.Business.User, {as: 'user', foreignKey: 'user_id'});
        app.model.Business.TravelOrder.belongsTo(app.model.Business.User, {as: 'shareUser', foreignKey: 'travel_share_user_id'});
        app.model.Business.TravelOrder.hasMany(app.model.Business.TravelOrderItem, {as: 'travelOrderItems', foreignKey: 'travel_order_id'});
        app.model.Business.TravelOrder.hasMany(app.model.Business.OrderLog, {as: 'orderLogs', foreignKey: 'order_id'});
        app.model.Business.TravelOrder.hasMany(app.model.Business.TravelerOrder, {as: 'travelerOrders', foreignKey: 'travel_order_id'});
        // 关联产品
        app.model.Business.TravelOrder.belongsTo(app.model.Business.Travel, {as: 'travel', foreignKey: 'travel_id'});
        // 关联sku
        app.model.Business.TravelOrder.belongsTo(app.model.Business.TravelSku, {as: 'travelSku', foreignKey: 'travel_sku_id'});
        // 关联团期
        app.model.Business.TravelOrder.belongsTo(app.model.Business.TravelSkuStock, {as: 'travelSkuStock', foreignKey: 'travel_sku_stock_id'});
    }
    return TravelOrder
}; 